<script setup>
import { authAdmin, menuSelectList, updateUser } from '../../../api'
import { onMounted, ref } from 'vue';
import dayjs from 'dayjs';
import { useRoute } from 'vue-router';
import PanelHead from '../../../components/panelHead.vue';

const route = useRoute()

const paginationData = ref({
  pageNum: 1,
  pageSize: 10
})

const tableData = ref({
  list: [],
  total: 0
})

onMounted(() => {
  getListData()
  menuSelectList().then(res => {
    // console.log(res)
    option.value = res.data.data
  })
})

//请求列表
const getListData = () => {
  authAdmin(paginationData.value.pageNum, paginationData.value.pageSize).then(res => {
    // console.log(res.data)
    const { list, total } = res.data.data
    list.forEach(item => {
      item.create_time = dayjs(item.create_time).format('YYYY-MM-DD')
    })
    tableData.value.list = list
    tableData.value.total = total
  })
}

const option = ref([])
//根据权限id匹配权限名称
const permissionName = (id) => {
  const data = option.value.find(el => el.id === id)
  return data ? data.name : '超级管理员'
}
const open = (rowData) => {
  // console.log(rowData)
  dialogFoemVisable.value = true
  Object.assign(form.value, { mobile: rowData.mobile, name: rowData.name, permissions_id: rowData.permissions_id })
}

const handleSizeChange = (value) => {
  paginationData.value.pageSize = value
  getListData()
}

const handleCurrentChange = (value) => {
  paginationData.value.pageNum = value
  getListData()
}

//弹窗
const dialogFoemVisable = ref(false)
const beforeClose = () => {
  dialogFoemVisable.value = false
}

const rules = ref({
  name: [{ required: true, trigger: 'blur', message: '请填写昵称' }],
  permissions_id: [{ required: true, trigger: 'blur', message: '请选择菜单权限' }]
})

//编辑表单
const formRef = ref()
const form = ref({
  name: '',
  permissions_id: ''
})

//表单提交
const confirm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      const { name, permissions_id } = form.value
      updateUser({ name, permissions_id }).then(res => {
        if (res.data.code === 10000) {
          dialogFoemVisable.value = false
          getListData()
        }
      })
      ElMessage.success('下一次时登陆生效')
    } else {
      console.log('error submit', fields)
    }
  })
}
</script>

<template>
  <PanelHead :route="route"></PanelHead>
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="昵称"></el-table-column>
    <el-table-column prop="permissions_id" label="所属组别">
      <template #default="scope">
        {{ permissionName(scope.row.permissions_id) }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号"></el-table-column>
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active ? 'success' : 'danger'">{{ scope.row.active ? '正常' : '失效' }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon>
            <Clock />
          </el-icon>
          <span style="margin-left: 10px;">{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination size="small" v-model:current-page="paginationData.pageNum" :page-size="paginationData.pageSize"
      :background="false" layout="total, prev, pager, next" :total="tableData.total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <el-dialog v-model="dialogFoemVisable" :before-close="beforeClose" title="添加权限" width="500">
    <el-form ref="formRef" :rules="rules" label-width="100px" label-position="left" :model="form">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" disabled></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select v-model="form.permissions_id" placeholder="请选择菜单权限" style="width: 240px;">
          <el-option v-for="item in option" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">
.flex-box {
  display: flex;
  align-items: center;
}
</style>